<table cellspacing="1" cellpadding="1" width='500'>
  <tr>
    <th class='s'></th>
    <th>存放点</th>
    <th>规格</th>
    <th class='m'>可订数</th>
    <th class='m'>预订数</th>
  </tr>
  <% @stats.each_with_index do |stat,index| %>
  <tr >
    <td class='s'></td>            
    <td><%= stat.dept.name %></td>
    <td><%= stat.furniture.spec %></td>
    <td class='r'><%= stat.spots_orderedable_count(:order_item_id => @order_item.try(:id)) %></td>
    <td class='c'>
      <%= hidden_field_tag :'stat_id[]',stat.id %>
      <%= hidden_field_tag :'stat_order_item_id[]',stat.stat_order_item_by_order_item_id(@order_item.try(:id)).try(:id) %>
      <%= link_to_function '-','', :id => "sub_order_count_#{stat.id}" %>
      <%= text_field_tag :'ordered_count[]',
        params[:ordered_count].present? ? params[:ordered_count][index] : (
            stat.stat_order_item_by_order_item_id(params[:id]).try(:ordered_count) || 0
          ), 
          :id => "order_count_#{stat.id}", 
          :class => :ordered_count , 
          :readonly => true%>              
      <%= link_to_function '+','', :id => "add_order_count_#{stat.id}" %>
      <script type="text/javascript" charset="utf-8">
        $(document).ready(function(){
          $('#add_order_count_<%= stat.id %>').click(function(){
            if($('#order_count_<%= stat.id%>').val() < <%= stat.spots_orderedable_count :order_item_id => params[:id] %> ) {
              $('#order_count_<%= stat.id%>').val(Number($('#order_count_<%= stat.id%>').val()) + 1);
              $('#ordered_furnitures_count').html(Number($('#ordered_furnitures_count').html()) + 1);
            }
          });
          $('#sub_order_count_<%= stat.id %>').click(function(){
            if($('#order_count_<%= stat.id%>').val() > 0 ) {
              $('#order_count_<%= stat.id%>').val(Number($('#order_count_<%= stat.id%>').val()) - 1);
              $('#ordered_furnitures_count').html(Number($('#ordered_furnitures_count').html()) - 1);
            }
          });
        });
      </script>
    </td>
  </tr>
  <% end %>
  <% if @stats.empty? %>
  <tr>
    <td colspan='5' class='c'>
      无现货库存，请直接向<%= link_to '厂家订做', new_order_made_order_item_order_url(@order), :remote => true, :method => :get %>。
    </td>
  </tr>
  <% else %>
  <tr>
    <td colspan='5' class='r'>
      可订现货共<b><%= @spots_orderedable_count %></b>件,预订共<b id='ordered_furnitures_count'><%= @order_item.present? ? @order_item.ordered_count : (params[:ordered_count].present? ? params[:ordered_count].inject(0) { |sum, count| sum + count.to_i } : 0) %></b>件
    </td>
  </tr>
  <% end %>
</table>